﻿Public Class QLKeHoach
    Dim kt As Boolean = True
    Private Sub Load_grvKeHoach()
        Dim tblKehoach As DataTable = LT_BANG.Doc("select KH.MAKH,KH.NAM,DV.TENDV from KEHOACH KH, DONVI DV where KH.MADV=DV.MADV")
        grvKeHoach.DataSource = tblKehoach
        grvKeHoach.Columns(0).HeaderText = "Mã KH"
        grvKeHoach.Columns(1).HeaderText = "Năm"
        grvKeHoach.Columns(2).HeaderText = "Đơn vị"

        grvKeHoach.Columns(0).Width = "50"
        grvKeHoach.Columns(1).Width = "120"
        grvKeHoach.Columns(2).Width = "430"
        grvKeHoach.AllowUserToAddRows = False
        grvKeHoach.EditMode = DataGridViewEditMode.EditProgrammatically
    End Sub

    Private Sub QLKeHoach_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        btnThem.Enabled = True
        btnXoa.Enabled = True
        btnLuu.Enabled = False
        btnSua.Enabled = True
        btnThoat.Enabled = True
        btnBoQua.Enabled = False
        Load_grvKeHoach()
        LT_BANG.Fillcombo("select MADV,TENDV from DONVI", cbDonVi, "MADV", "TENDV")
        cbDonVi.SelectedIndex = -1
        grpKeHoach.Enabled = False
        ResetValues()
    End Sub
    Private Sub ResetValues()
        txtNam.Text = ""
        cbDonVi.Text = ""
        txtTuGT.Text = "0"
        txtClo.Text = "0"
        txtDien.Text = "0"
        txtDTDH.Text = "0"
        txtDTOL.Text = "0"
        txtDTON.Text = "0"
        txtDTSX.Text = "0"
        txtMuaGT.Text = "0"
        txtMuaSX.Text = "0"
        txtPhen.Text = "0"
        txtTuSX.Text = "0"
        txtVoi.Text = "0"
        txtXut.Text = "0"
    End Sub

    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Me.Close()
    End Sub

    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        kt = True
        btnThem.Enabled = False
        btnLuu.Enabled = True
        btnSua.Enabled = False
        btnThoat.Enabled = True
        grpKeHoach.Enabled = True
        txtNam.ReadOnly = False
        grvKeHoach.Enabled = False
        btnBoQua.Enabled = True
        ResetValues()
    End Sub

    Private Sub btnLuu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLuu.Click
        Dim Bang As DataTable = LT_BANG.Doc("select * from KEHOACH")
        If kt = True Then
            Dim KH_moi As DataRow = Bang.NewRow
            KH_moi("NAM") = txtNam.Text
            KH_moi("MADV") = cbDonVi.SelectedValue
            KH_moi("SANXUAT") = Integer.Parse(txtTuSX.Text)
            KH_moi("GHITHU") = Integer.Parse(txtTuGT.Text)
            KH_moi("KHMUASX") = Integer.Parse(txtMuaSX.Text)
            KH_moi("KHMUAGHITHU") = Integer.Parse(txtMuaGT.Text)
            KH_moi("DIEN") = Double.Parse(txtDien.Text)
            KH_moi("PHEN") = Double.Parse(txtPhen.Text)
            KH_moi("VOI") = Double.Parse(txtVoi.Text)
            KH_moi("CLO") = Double.Parse(txtClo.Text)
            KH_moi("XUT") = Double.Parse(txtXut.Text)
            KH_moi("DTSANXUAT") = Integer.Parse(txtDTSX.Text)
            KH_moi("DTMUA") = Integer.Parse(txtDTDH.Text)
            KH_moi("DTONGNHANH") = Integer.Parse(txtDTON.Text)
            KH_moi("DTONGLON") = Integer.Parse(txtDTOL.Text)
            Bang.Rows.Add(KH_moi)
        Else
            Dim i As Integer = grvKeHoach.SelectedCells(0).RowIndex
            Bang.Rows(i)(1) = txtNam.Text
            Bang.Rows(i)(2) = cbDonVi.SelectedValue
            Bang.Rows(i)(3) = Integer.Parse(txtTuSX.Text)
            Bang.Rows(i)(4) = Integer.Parse(txtTuGT.Text)
            Bang.Rows(i)(5) = Integer.Parse(txtMuaSX.Text)
            Bang.Rows(i)(6) = Integer.Parse(txtMuaGT.Text)
            Bang.Rows(i)(7) = Double.Parse(txtDien.Text)
            Bang.Rows(i)(8) = Double.Parse(txtPhen.Text)
            Bang.Rows(i)(9) = Double.Parse(txtVoi.Text)
            Bang.Rows(i)(10) = Double.Parse(txtClo.Text)
            Bang.Rows(i)(11) = Double.Parse(txtXut.Text)
            Bang.Rows(i)(12) = Integer.Parse(txtDTSX.Text)
            Bang.Rows(i)(13) = Integer.Parse(txtDTDH.Text)
            Bang.Rows(i)(14) = Integer.Parse(txtDTON.Text)
            Bang.Rows(i)(15) = Integer.Parse(txtDTOL.Text)

        End If
        LT_BANG.Ghi(Bang, "KEHOACH")
        Load_grvKeHoach()
        ResetValues()
        btnThem.Enabled = True
        btnLuu.Enabled = False
        btnSua.Enabled = True
        btnBoQua.Enabled = False
        grpKeHoach.Enabled = False

    End Sub

    Private Sub grvKeHoach_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grvKeHoach.CellClick
        If grvKeHoach.SelectedCells.Count > 0 Then
            Dim i As Integer = grvKeHoach.SelectedCells(0).RowIndex
            Dim bang As DataTable = LT_BANG.Doc("select * from KEHOACH")
            If bang.Rows.Count > 0 Then
                txtNam.Text = bang.Rows(i)(1).ToString()
                cbDonVi.SelectedValue = bang.Rows(i)(2).ToString()
                txtTuSX.Text = bang.Rows(i)(3).ToString()
                txtTuGT.Text = bang.Rows(i)(4).ToString()
                txtMuaSX.Text = bang.Rows(i)(5).ToString()
                txtMuaGT.Text = bang.Rows(i)(6).ToString()
                txtDien.Text = bang.Rows(i)(7).ToString()
                txtPhen.Text = bang.Rows(i)(8).ToString()
                txtVoi.Text = bang.Rows(i)(9).ToString()
                txtClo.Text = bang.Rows(i)(10).ToString()
                txtXut.Text = bang.Rows(i)(11).ToString()
                txtDTSX.Text = bang.Rows(i)(12).ToString()
                txtDTDH.Text = bang.Rows(i)(13).ToString()
                txtDTON.Text = bang.Rows(i)(14).ToString()
                txtDTOL.Text = bang.Rows(i)(15).ToString()
            End If
        End If
    End Sub

    Private Sub btnSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSua.Click
        kt = False
        If txtNam.Text = "" Then
            MessageBox.Show("Vui lòng chọn năm cần sửa", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return
        End If
        btnSua.Enabled = False
        btnThem.Enabled = False
        btnLuu.Enabled = True
        btnBoQua.Enabled = True
        grpKeHoach.Enabled = True
        txtNam.ReadOnly = True
    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click

    End Sub
    Private Sub txtTuSX_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtTuSX.TextChanged
        Dim klsx, klmuasx, tongklsx As Long
        'Dim i As Integer = 350

        If txtTuSX.Text = "" Then
            klsx = 0
        Else
            klsx = Long.Parse(txtTuSX.Text)
        End If

        lblTuSX.Text = String.Format("{0:0,0}", klsx * 350)
        If txtMuaSX.Text = "" Then
            klmuasx = 0
        Else
            klmuasx = Long.Parse(txtMuaSX.Text)
        End If
        lblMuaSX.Text = String.Format("{0:0,0}", klmuasx * 350)
        tongklsx = (klsx + klmuasx) * 350
        lblTongSX.Text = String.Format("{0:0,0}", tongklsx)
    End Sub

    Private Sub txtMuaSX_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtMuaSX.TextChanged
        Dim klsx, klmuasx, tongklsx As Long
        'Dim i As Integer = 350
        'txtTuSX.Text = String.Format("{0:0,0}", Integer.Parse(txtTuSX.Text))
        If txtTuSX.Text = "" Then
            klsx = 0
        Else
            klsx = Long.Parse(txtTuSX.Text)
        End If
        lblTuSX.Text = String.Format("{0:0,0}", klsx * 350)
        If txtMuaSX.Text = "" Then
            klmuasx = 0
        Else
            klmuasx = Long.Parse(txtMuaSX.Text)
        End If
        lblMuaSX.Text = String.Format("{0:0,0}", klmuasx * 350)
        tongklsx = (klsx + klmuasx) * 350
        lblTongSX.Text = String.Format("{0:0,0}", tongklsx)
    End Sub

    Private Sub btnBoQua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBoQua.Click
        ResetValues()
        btnThem.Enabled = True
        btnSua.Enabled = True
        btnBoQua.Enabled = False
        btnLuu.Enabled = False
        grvKeHoach.Enabled = True
        grpKeHoach.Enabled = False
    End Sub

    Private Sub txtTuGT_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtTuGT.TextChanged
        Dim gtsx, gtmua, tonggt As Long
        Dim tltt As Double
        If txtTuGT.Text = "" Then
            gtsx = 0
        Else
            gtsx = Long.Parse(txtTuGT.Text)
        End If

        If txtMuaGT.Text = "" Then
            gtmua = 0
        Else
            gtmua = Long.Parse(txtMuaGT.Text)
        End If
        tonggt = gtsx + gtmua
        lblTongGT.Text = String.Format("{0:0,0}", tonggt)
    End Sub

    
    Private Sub txtMuaGT_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtMuaGT.TextChanged
        Dim gtsx, gtmua, tonggt As Long

        If txtTuGT.Text = "" Then
            gtsx = 0
        Else
            gtsx = Long.Parse(txtTuGT.Text)
        End If

        If txtMuaGT.Text = "" Then
            gtmua = 0
        Else
            gtmua = Long.Parse(txtMuaGT.Text)
        End If
        tonggt = gtsx + gtmua
        lblTongGT.Text = String.Format("{0:0,0}", tonggt)
    End Sub

    Private Sub txtDTSX_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDTSX.TextChanged
        Dim dtsx, dtmua, tongdt, dtbq As Long
        Dim gtsx, gtmua, tonggt As Long

        If txtTuGT.Text = "" Then
            gtsx = 0
            tonggt = 1
        Else
            gtsx = Long.Parse(txtTuGT.Text)
        End If

        If txtMuaGT.Text = "" Then
            gtmua = 0
            tonggt = 1
        Else
            gtmua = Long.Parse(txtMuaGT.Text)
        End If
        If txtDTSX.Text = "" Then
            dtsx = 0
        Else
            dtsx = Long.Parse(txtDTSX.Text)
        End If
        If txtDTDH.Text = "" Then
            dtmua = 0
        Else
            dtmua = Long.Parse(txtDTDH.Text)
        End If
        If gtmua = 0 And gtsx = 0 Then
            tonggt = 1
        Else
            tonggt = gtsx + gtmua
        End If
        dtbq = (dtsx + dtmua) * 1000000 / tonggt
        tongdt = dtsx + dtmua
        lblDTNuoc.Text = String.Format("{0:0,0}", tongdt)
        lblDTBQ.Text = String.Format("{0:0,0}", dtbq)
    End Sub

    Private Sub txtDTDH_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDTDH.TextChanged
        Dim dtsx, dtmua, tongdt, dtbq As Long
        Dim gtsx, gtmua, tonggt As Long

        If txtTuGT.Text = "" Then
            gtsx = 0
            tonggt = 1
        Else
            gtsx = Long.Parse(txtTuGT.Text)
        End If

        If txtMuaGT.Text = "" Then
            gtmua = 0
            tonggt = 1
        Else
            gtmua = Long.Parse(txtMuaGT.Text)
        End If
        If txtDTSX.Text = "" Then
            dtsx = 0
        Else
            dtsx = Long.Parse(txtDTSX.Text)
        End If
        If txtDTDH.Text = "" Then
            dtmua = 0
        Else
            dtmua = Long.Parse(txtDTDH.Text)
        End If
        If gtmua = 0 And gtsx = 0 Then
            tonggt = 1
        Else
            tonggt = gtsx + gtmua
        End If

        dtbq = (dtsx + dtmua) * 1000000 / tonggt
        tongdt = dtsx + dtmua
        lblDTNuoc.Text = String.Format("{0:0,0}", tongdt)
        lblDTBQ.Text = String.Format("{0:0,0}", dtbq)
    End Sub

    Private Sub txtDTON_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDTON.TextChanged
        Dim dtsx, dtmua, dt_ongnhanh, dt_onglon, tongdt As Long
        If txtDTSX.Text = "" Then
            dtsx = 0
        Else
            dtsx = Long.Parse(txtDTSX.Text)
        End If
        If txtDTDH.Text = "" Then
            dtmua = 0
        Else
            dtmua = Long.Parse(txtDTDH.Text)
        End If
        If txtDTON.Text = "" Then
            dt_ongnhanh = 0
        Else
            dt_ongnhanh = Long.Parse(txtDTON.Text)
        End If
        If txtDTOL.Text = "" Then
            dt_onglon = 0
        Else
            dt_onglon = Long.Parse(txtDTOL.Text)
        End If
        tongdt = dtsx + dtmua + dt_ongnhanh + dt_onglon

        lblTongDT.Text = String.Format("{0:0,0}", tongdt)
    End Sub

    Private Sub txtDTOL_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDTOL.TextChanged
        Dim dtsx, dtmua, dt_ongnhanh, dt_onglon, tongdt As Long
        If txtDTSX.Text = "" Then
            dtsx = 0
        Else
            dtsx = Long.Parse(txtDTSX.Text)
        End If
        If txtDTDH.Text = "" Then
            dtmua = 0
        Else
            dtmua = Long.Parse(txtDTDH.Text)
        End If
        If txtDTON.Text = "" Then
            dt_ongnhanh = 0
        Else
            dt_ongnhanh = Long.Parse(txtDTON.Text)
        End If
        If txtDTOL.Text = "" Then
            dt_onglon = 0
        Else
            dt_onglon = Long.Parse(txtDTOL.Text)
        End If
        tongdt = dtsx + dtmua + dt_ongnhanh + dt_onglon
        lblTongDT.Text = String.Format("{0:0,0}", tongdt)
    End Sub
End Class